//
//  EventAnnouncedCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class EventAnnouncedCell: BaseTableViewCell {
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var lineView: UIView!
    
    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var ownerLabel: UILabel!
    @IBOutlet weak var IPLabel: UILabel!
    @IBOutlet weak var lukNumberLabel: UILabel!
    @IBOutlet weak var lastLabel: UILabel!
    
    var model : SurpriseShopDetModel?{
    
        didSet{
        
            if model == nil{
            
            return
                
            }
            
            
            topLabel.attributedText = attributedString2(str: "期号: \(model!.od_no) 揭晓时间: \(model!.publish_date)")
        
            ownerLabel.attributedText = attributedString(str: "获奖者: \(model!.member_name)", color: UIColor.themeColor())
            
            IPLabel.text = "\(model!.province)IP: \(model!.member_ip)"
            
            lukNumberLabel.text = "幸运号码: \(model!.prize_code)"
            
            lastLabel.attributedText = attributedString(str: "本次参与: \(model!.buy_count)人次", color: UIColor.red)
            
                
        }
        
        
    
    }
    
    
    
    
    func attributedString(str: String, color: UIColor) -> NSAttributedString? {
        
        
        
        //创建正则表达式对象
        let patten0 = "[\u{4e00}-\u{9fa5}]{1,}"
        //创建正则表达式对象
        let patten1 = "[:]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: color, range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:temp.range )
            
        }
        
        return strM
        
    }
    
    
    
    
    
    
    func attributedString2(str: String) -> NSAttributedString? {
        
        //编写规则
        let patten = "[0-9]{1,15}"
        //创建正则表达式对象
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res = regex.firstMatch(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range:  NSRange(location: 0, length: str.characters.count))
        
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 15), range: NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSMakeRange(0, str.characters.count))
        
        
        //        let paragr = NSMutableParagraphStyle.init()
        //
        //        paragr.firstLineHeadIndent = 6;
        //
        //        paragr.lineSpacing = 2;
        
        //        strM.addAttribute(NSParagraphStyleAttributeName, value: paragr, range: NSMakeRange(0, str.characters.count))
        
        if let temp = res?.range {
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: temp)
            
        }
        
        return strM
        
        
        
        
    }

    
    
    

    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.viewBgGrey()
        
        leftImageView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 30)
        
        lineView.backgroundColor = UIColor.lineGray()
        
        leftImageView.image = UIImage.defaulIconImage()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
